﻿using LbangCMS.EFModels;
using LbangCMS.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LbangCMS.Controllers
{
    public class VideoController : Controller
    {
        // GET: Video
        public ActionResult Index(int categoryId=0)
        {
            var model = new VideoIndexModel();
            model.VideoCategories = new List<VideoCategoryModel>();
            var categories = GetAllCategory();
            if (categories != null && categories.Any())
            {
                foreach (var item in categories)
                {
                    model.VideoCategories.Add(new VideoCategoryModel() { ID = item.ID, Name = item.Name, SortIndex = item.SortIndex });
                }
            }
            if (categoryId == 0 && categories.Any())
            {
                categoryId = categories.OrderBy(o => o.SortIndex).First().ID;
            }
            var category = GetCategory(categoryId);
            if(category==null)
            {
                return RedirectToAction("NotFound", "Home");
            }
            else
            {
                
                model.CurrentCategoryId = categoryId;
                model.CurrentCategoryName = category.Name;
                
                
                var videos = GetVideoList(categoryId);
                model.VideoList = new List<VideoModel>();
                if(videos!=null&&videos.Any())
                {
                    foreach (var item in videos)
                    {
                        model.VideoList.Add(new VideoModel() { ID=item.ID,ImageSrc=item.ImageSrc,Title=item.Name });
                    }
                }
            }
            return View(model);
        }

        public ActionResult Detail(int id)
        {
            var model = new VideoDetailModel();
            var video = GetVideo(id);
            if(video==null)
            {
                return RedirectToAction("NotFound", "Home");
            }
            else
            {
                model.VideoCategories = new List<VideoCategoryModel>();
                var categories = GetAllCategory();
                if (categories != null && categories.Any())
                {
                    foreach (var item in categories)
                    {
                        model.VideoCategories.Add(new VideoCategoryModel() { ID = item.ID, Name = item.Name, SortIndex = item.SortIndex });
                    }
                }
                model.VideoId = id;
                model.Title = video.Name;
                model.Body = video.Body;
                model.PublishTime = video.CreateTime.ToString("yyyy-MM-dd");
                model.CurrentCategoryId = video.CategoryId;
                var category = GetCategory(video.CategoryId);
                model.CurrentCategoryName = category.Name;
                ViewBag.Title = model.Title;
            }
            return View(model);
        }

        #region private
        private Video GetVideo(int id)
        {
            using (var db = new Database())
            {
                return db.Videos.Where(o => o.ID == id).FirstOrDefault();
            }
        }

        public List<VideoCategory> GetAllCategory()
        {
            using (var db = new Database())
            {
                return db.VideoCategories.OrderBy(o => o.SortIndex).ToList();
            }
        }

        public List<Video> GetVideoList(int categoryId)
        {
            using (var db = new Database())
            {
                return db.Videos.Where(o => o.CategoryId == categoryId).ToList();
            }
        }

        public VideoCategory GetCategory(int categoryId)
        {
            using (var db = new Database())
            {
                return db.VideoCategories.Where(o => o.ID == categoryId).FirstOrDefault();
            }
        }
        #endregion
    }
}